package org.java;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DoDate {
	public static void getDate(String unixDate){
		SimpleDateFormat fm1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
		 SimpleDateFormat fm2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
		 long unixLong = 0;
		 String date = ""; 
		 unixLong = Long.parseLong(unixDate); 
		 date = fm1.format(unixLong);
		 System.out.println(date);
		 date = fm2.format(unixLong);
		 System.out.println(date); 
	}
	
	public static Date convertLocalTimestamp(long millis)
	{
	    TimeZone tz = TimeZone.getDefault();
	    Calendar c = Calendar.getInstance(tz);
	    long localMillis = millis;
	    int offset, time;

	    c.set(1970, Calendar.JANUARY, 1, 0, 0, 0);

	    // Add milliseconds
	    while (localMillis > Integer.MAX_VALUE)
	    {
	        c.add(Calendar.MILLISECOND, Integer.MAX_VALUE);
	        localMillis -= Integer.MAX_VALUE;
	    }
	    c.add(Calendar.MILLISECOND, (int)localMillis);

	    // Stupidly, the Calendar will give us the wrong result if we use getTime() directly.
	    // Instead, we calculate the offset and do the math ourselves.
	    time = c.get(Calendar.MILLISECOND);
	    time += c.get(Calendar.SECOND) * 1000;
	    time += c.get(Calendar.MINUTE) * 60 * 1000;
	    time += c.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000;
	    offset = tz.getOffset(c.get(Calendar.ERA), c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.DAY_OF_WEEK), time);

	    return new Date(millis - offset);
	}
	
	public static void main(String[] args) throws ParseException {
		Calendar calendar = Calendar.getInstance();		
		calendar.set(2013, 6, 1, 0, 0, 0);
		long timeInMillis = calendar.getTimeInMillis();
		System.out.println(timeInMillis);
		calendar.set(2013, 6, 2, 0, 0, 0);
		timeInMillis = calendar.getTimeInMillis();
		System.out.println(timeInMillis);
//		calendar.setTimeInMillis(millis);
		int c = 128;
		int x = c%48%8+1;
		int y = c%48/6;
		System.out.println("collection"+y+":shard"+x);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		sdf.setCalendar(calendar.toString());
		Date parse = sdf.parse(calendar.getTime().toLocaleString());
		System.out.println("====");
		
		System.out.println(convertLocalTimestamp(timeInMillis));
		
		System.out.println(new Date(timeInMillis));

	}
}

